package com.hengpeng.itfin.port.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.constant.Constant;
import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.itfin.common.MessageUtil;
import com.hengpeng.itfin.domain.annotation.RemoteService;
import com.hengpeng.itfin.domain.annotation.RemoteTransaction;
import com.hengpeng.itfin.exception.TransException;
import com.hengpeng.itfin.service.ServiceFactory;
import com.hengpeng.itfin.tools.SequenceGenerator;
import com.hengpeng.itfinbase.constants.RespCodeConstants;
import com.hengpeng.itfinbase.constants.TransCodeConstants;
import com.hengpeng.itfinbase.en.TradeMode;
import com.hengpeng.itfinbase.message.RequestMessage;
import com.hengpeng.itfinbase.message.ResponseMessage;
import com.hengpeng.itfinbase.message.partner.AddBidInfoRequest;
import com.hengpeng.itfinbase.message.partner.CreditAssignRequest;
import com.hengpeng.itfinbase.message.partner.CreditAssignResponse;
import com.hengpeng.itfinbase.message.partner.LoansRequest;
import com.hengpeng.itfinbase.message.partner.NotifyRequestMsg;
import com.hengpeng.itfinbase.message.partner.NotifyResponseMsg;
import com.hengpeng.itfinbase.message.partner.RedUseRequestMsg;
import com.hengpeng.itfinbase.message.partner.UserBankCardRequest;
import com.hengpeng.itfinbase.message.partner.UsrUnFreezeRequest;
import com.hengpeng.itfinbase.message.sequence.SequenceNoRequest;
import com.hengpeng.itfinbase.message.sequence.SequenceNoResponse;
import com.hengpeng.itfinbase.message.userInfo.AddMoneyRequest;
import com.hengpeng.itfinbase.message.userInfo.MobileBingRequest;
import com.hengpeng.itfinbase.message.userInfo.RealNameAuthRequest;
import com.hengpeng.itfinbase.message.userInfo.SendEmailRequest;
import com.hengpeng.itfinbase.message.userInfo.SendSMSRequest;
import com.hengpeng.itfinbase.message.userInfo.UserInfoRegisterRequest;
import com.hengpeng.itfinbase.message.userInfo.UserInfoResponse;
import com.hengpeng.itfinbase.message.userInfo.UserLevelUpRequest;
import com.hengpeng.itfinbase.message.userInfo.UserLoginRequest;
import com.hengpeng.itfinbase.message.userInfo.UserSetProtectPwdRequest;
import com.hengpeng.itfinbase.message.userInfo.UserUpdatePwdRequest;

@Service
@RemoteService
@Lazy
public class TransService {
    public final static String ERROR_MESSAGE = "系统异常";

    private static final Log logger = LogFactory.getLog(TransService.class);

    private static void errorLog(Throwable ex) {
        logger.error("BS服务调用异常。", ex);
    }

    @Autowired
    public ServiceFactory serviceFactory;
    
    @RemoteTransaction(transType = TransCodeConstants.RedPacketCode.REDPACKET_USE)
    public ResponseMessage redpacketUse(RedUseRequestMsg request) {
        try {
            return serviceFactory.getRedPacketService().investUseRedPacket(request.getRedPacketId());
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }
    

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_REGISTER)
    public UserInfoResponse register(UserInfoRegisterRequest request) {
        try {
            return serviceFactory.getUserInfoService().register(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_LEVEL_UP)
    public ResponseMessage memberLevelUp(UserLevelUpRequest request) {
        try {
            return serviceFactory.getUserInfoService().userLevelUp(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_LOGIN)
    public UserInfoResponse login(UserLoginRequest request) {
        try {
            return serviceFactory.getUserInfoService().login(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.TradeTransCode.TRADE_CREATEASSIGEN)
    public CreditAssignResponse createAssign(CreditAssignRequest request) {
        try {
            return serviceFactory.getAssignService().createAssign(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(CreditAssignResponse.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(CreditAssignResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.PartnerTransCode.REQUEST)
    public ResponseMessage partnerFormRequest(RequestMessage request) {
        try {
            return serviceFactory.getPartnerService().request(request);

        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.PartnerTransCode.NOTIFY)
    public NotifyResponseMsg dealNotify(NotifyRequestMsg notifyMsg) {
        try {
            return serviceFactory.getPartnerService().dealNotify(notifyMsg);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(NotifyResponseMsg.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(NotifyResponseMsg.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.ISTRUSTPATTER)
    public ResponseMessage isTrustPattern(RequestMessage request) {
        try {
            ResponseMessage response = new ResponseMessage();
            boolean bol = serviceFactory.getPartnerService().isTrustPattern();
            response.setReturnObject(bol);
            return response;
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.OtherTransCode.SEQUENCE_NO)
    public SequenceNoResponse getSequenceNo(SequenceNoRequest request) {
        try {
            String sequenceNo = SequenceGenerator.createSequenceNo(
                    request.getPrefix(),
                    request.getSuffix(),
                    request.getLength(),
                    serviceFactory.getSequenceService().getSequence(request.getSequenceName(),
                            request.getSequenceCount(), request.getModMaxNum()));
            SequenceNoResponse response = new SequenceNoResponse();
            response.setSequenceNo(sequenceNo);
            return response;
        } catch (TransException ms) {
            errorLog(ms);
            return MessageUtil.getErrorResponseMessage(SequenceNoResponse.class, ms.getExceptionCode(),
                    ms.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(SequenceNoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.BidTransCode.BID_ADDBIDINFO_REQUEST)
    public ResponseMessage addBidInfo(AddBidInfoRequest request) {
        try {
            return serviceFactory.getBorrowService().addBidInfo(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.BidTransCode.BID_LOANS_REQUEST)
    public ResponseMessage loans(LoansRequest request) {
        try {
            ServiceResult<Boolean> result = serviceFactory.getBorrowService().loans(request);
            return convertResult(result);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.BidTransCode.BID_LOSS_REQUEST)
    public ResponseMessage loss(UsrUnFreezeRequest request) {
        try {
            ServiceResult<Boolean> result = serviceFactory.getBorrowService().loss(request);
            return convertResult(result);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    private ResponseMessage convertResult(ServiceResult<?> result) {
        return convertResult(result, null);
    }

    private ResponseMessage convertResult(ServiceResult<?> result, ResponseMessage response) {
        if (response == null) {
            response = new ResponseMessage();
        }
        if (!result.getIsSuccess()) {
            if (result.getCode().equals(RespCodeConstants.CommonCode.TRANS_SUC)) {
                response.setRespCode(RespCodeConstants.CommonCode.TRANSMESSAGE_ERRCODE_UNKOWN);
            } else {
                response.setRespCode(result.getCode());
            }
        }
        return response;
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_UPDATE_PWD)
    public UserInfoResponse updatePassword(UserUpdatePwdRequest request) {
        try {
            return serviceFactory.getUserInfoService().updatePassword(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    /**
     * add by lugaofei 20151022 找回密码
     * 
     */
    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_BACK_PWD)
    public UserInfoResponse backPassword(UserUpdatePwdRequest request) {
        try {
            return serviceFactory.getUserInfoService().backPassword(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_REALNAME_AUTH)
    public UserInfoResponse realNameAuth(RealNameAuthRequest request) {
        try {
            return serviceFactory.getUserInfoService().realNameAuth(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_SET_PROTECT_PWD)
    public UserInfoResponse doSetQuestionPwd(UserSetProtectPwdRequest request) {
        try {
            return serviceFactory.getUserInfoService().doSetQuestionPwd(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_VALID_EMAIL)
    public ResponseMessage sendEmailForValidEmail(SendEmailRequest request) {
        try {
            ResponseMessage result = serviceFactory.getEmailService().sendValidEmail(request.getUrl(),
                    request.getUsername(), request.getEmail(), request.getActiveCode(), request.getSuccessUrl(),
                    request.getErrorUrl(), request.getType());
            return result;
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    // @RemoteTransaction(transType =
    // TransCodeConstants.MemberTransCode.MEMBER_ACTIVE_EMAIL)
    // public ResponseMessage sendEmailForActiveEmail(SendEmailRequest request)
    // {
    // try
    // {
    // ResponseMessage result =
    // serviceFactory.getEmailService().sendActiveEmail(request.getUrl(),
    // request.getUsername(), request.getEmail(),
    // request.getActiveCode(),request.getReturnUrl(), request.getType());
    // return result;
    // }
    // catch (TransException tex)
    // {
    // errorLog(tex);
    // return MessageUtil.getErrorResponseMessage(ResponseMessage.class,
    // tex.getExceptionCode(),
    // tex.getExceptionMsg());
    // }
    // catch (Exception ex)
    // {
    // errorLog(ex);
    // return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
    // Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
    // }
    // }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_VALID_EMAIL_AFFIRM)
    public ResponseMessage confirmEmailForValid(SendEmailRequest request) {
        try {
            ResponseMessage result = serviceFactory.getEmailService().confirmEmailForValid(request);
            return result;
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_VALID_MOBILE)
    public ResponseMessage sendValidSMS(SendSMSRequest request) {
        try {
            ResponseMessage result = serviceFactory.getSmsService().sendValidSMS(request);
            return result;
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_VALID_MOBILE_AFFIRM)
    public ResponseMessage confirmValidSMS(SendSMSRequest request) {
        try {
            ResponseMessage result = serviceFactory.getSmsService().confirmValidSMS(request);
            return result;
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_BANKCARD_SAVE)
    public ResponseMessage saveUserBankCard(UserBankCardRequest request) {
        try {
            ResponseMessage result = serviceFactory.getUserInfoService().saveUserBankCard(request.getBankCard());
            return result;
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_BANKCARD_BD)
    public ResponseMessage userBindCard(UserBankCardRequest request) {
        try {
            ResponseMessage result = serviceFactory.getUserInfoService().userBindCard(request.getBankCard());
            return result;
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_BANKCARD_UPDATE)
    public ResponseMessage updateUserBindCard(UserBankCardRequest request) {
        try {
            ResponseMessage result = serviceFactory.getUserInfoService().updateUserBindCard(request.getBankCard());
            return result;
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_BANKCARD_REMOVE)
    public ResponseMessage removeUserBindCard(UserBankCardRequest request) {
        try {
            ResponseMessage result = serviceFactory.getUserInfoService().removeUserBindCard(
                    request.getBankCard().getId());
            return result;
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.OtherTransCode.ADD_MONEY)
    public ResponseMessage addMoney(AddMoneyRequest request) {
        try {
            serviceFactory.getAccountService().changeAccountMoney(request.getUserId(), request.getMoney(), 0l, 0l, 0l,
                    request.getType(), TradeMode.ADD, request.getReferenceId(), request.getRemark());
            // changeAccountMoney(request.getUserId(), request.getMoney(),
            // request.getType(), TradeMode.ADD, request.getReferenceId(),
            // request.getRemark());
            return new ResponseMessage();
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.MemberTransCode.MEMBER_BING_MOBILE)
    public ResponseMessage addMoney(MobileBingRequest request) {
        try {
            return serviceFactory.getUserInfoService().bingMobile(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }
}
